package com.starblink.android.basic.ext

import android.net.Uri
import com.starblink.basic.log.SKLogger


/**
 * 获取Uri参数Map
 */
fun Uri.getQueryParameterMap(): Map<String, String> {
    val query: String = encodedQuery ?: return emptyMap<String, String>()

    val parameterMap = HashMap<String, String>()
    var start = 0
    do {
        val next = query.indexOf('&', start)
        val end = if (next == -1) query.length else next
        var separator = query.indexOf('=', start)
        if (separator > end || separator == -1) {
            separator = end
        }
        val key = query.substring(start, separator)
        val value = if (separator < end) {
            query.substring(separator + 1, end)
        } else {
            "null"
        }
        parameterMap[Uri.decode(key)] = Uri.decode(value)
        // Move start to end of name.
        start = end + 1
    } while (start < query.length)

    SKLogger.e("dss", "Uri.getQueryParameterMap()，query=${query},parameterMap=${parameterMap}")
    return parameterMap
}


